﻿using Microsoft.Extensions.Configuration;
using Surpass.Model.SysDomain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Surpass.Model.Dto.ResponseDto.ResponseModel.MenuModel;

namespace Surpass.DapperRepository.Repository
{
    public class MemuRepository
    {
        private readonly IConfiguration _configuration;
        public MemuRepository(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        public List<MenuResponse> GetMenus()
        {
            using (IDbConnection conn = new SqlConnection(_configuration.GetConnectionString("MsSqlServer")))
            {
                var list =  conn.Query<SysMenu>("select * from Sys_Menu").ToList();
                return GetMenuResponses(list);
            }
        }

        public List<MenuResponse> GetMenuResponses(List<SysMenu> sysMenus,string parentMenuId="")
        {
            return sysMenus.Where(u => u.Type == Config.SysConstants.MenuType.Menu && u.Deleted == false && u.ParentMenuId == parentMenuId).Select(u => new MenuResponse
            {
                Icon = u.Icon,
                Type = u.Type,
                Level = u.Level,
                MenuId = u.MenuId,
                MenuName = u.MenuName,
                OrderIndex = u.OrderIndex,
                ParentMenuId = parentMenuId,
                Url = u.Url,
                ChildMenus = GetMenuResponses(sysMenus, u.MenuId).Count == 0 ? null : GetMenuResponses(sysMenus, u.MenuId)
            }).ToList();
        }
    }
}
